﻿
package com.ostrich.manpower.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.StringUtil;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrSocialsecurity;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.common.StringUtils;
import com.ostrich.manpower.service.TostHrSocialsecurityDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrSocialsecurityService")
public class TostHrSocialsecurityService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrSocialsecurityService.class);

	@Autowired
    private TostHrSocialsecurityDao<T> dao;
	
	@Autowired
	private TostHrBaseinfoService tosthrbaseinfoservice;

	public TostHrSocialsecurityDao<T> getDao() {
		return dao;
	}
	public List<T> queryByAll() throws Exception{
		return getDao().queryByAll();
	}
	
	public List<T> queryScoci(int hrid){
		return dao.queryScocialsecurity(hrid);
	}
	
	
	public List<T> queryScocial(@Param("hmeMonth")String hmeMonth,@Param("hrId")int hrId){
		return dao.queryScocial(hmeMonth,hrId);
	}
	/****
	 * 根据sheet插入数据库
	 * @param sheet
	 * @return
	 */
	public String setsocialBySheet(HSSFSheet sheet,String month) throws Exception{
		//List<TostHrMealaddinfo> infos =new ArrayList<TostHrMealaddinfo>();
	    HSSFRow row = null;
	    int colnum = 9;
	    String msg="";
		 //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数  除去标题跟表头
	     for(int j = 2; j < sheet.getPhysicalNumberOfRows(); j++) {  
	         row = sheet.getRow(j);  
	         TostHrSocialsecurity social = new TostHrSocialsecurity();  
	         String iscontinut1 = (row!=null)?ImportUtil.getCellValue(row.getCell(1)):null;
        	 TostHrBaseinfo pid= (TostHrBaseinfo) tosthrbaseinfoservice.getBaseinfoByHrCode(iscontinut1);
        	 
        	 String personname=ImportUtil.getCellValue(row.getCell(3));
        	 personname=(personname!=null && personname.trim().length()>0)?personname:"";
        	 //判断没有这个用户将不进行数据导入。
        	 if(pid==null || pid.getId()==null){
        		 msg +="第"+(j+1)+"行记录异常：工号："+iscontinut1+"，姓名为："+personname+"的社保资料没有正常导入（工号不存在）。";
				continue;
			}
        	 
        	 
	         if(iscontinut1!=null && iscontinut1.length()>0 && personname.equalsIgnoreCase(pid.getHrName()) ){
		         for(int i=0;i<colnum;i++ ){//i代表列数
		        	 String str = ImportUtil.getCellValue(row.getCell(i));
			         if(i==1){//id
			        		 social.setHrId(pid.getId());
			        		 continue;
			         }else if(i==2){//编号
		        		 social.setHsoPercoding(str);continue;
		        	 }else if(i==3){//姓名
			        		 social.setHsoName(str);continue;
		        	 }else if(i==4){//缴费工资
		        		 Float flstr = (str !=null && str.trim().length()>0 &&
		        				 str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		 //Float flstr = (StringUtils.isNum(str))?Float.valueOf(str):new Float(0);
		        		 social.setHsoSalary(flstr);continue;
		        	 }else if(i==5){//养 老 保 险月数
		        		 social.setHsoEn2month(str);continue;
		        	 }else if(i==6){//医 疗 保 险月数
		        		 social.setHsoMe2month(str);continue;
		        	 }else if(i==7){//失业保险月数
		        		 social.setHsoUn2month(str);continue;
		        	 }else if(i==8){//工伤保险月数
		        		 social.setHsoIn2month(str);continue;
		        	 }
		         }
	         
	         if(social!=null && social.getHsoName()!=null && social.getHsoName().trim().length()>0){
	        	  Map<String,Object> fMap = FormulaUtil.formulaById(7, social.getHsoSalary(),social.getHsoEn2month(), social.getHsoMe2month(),social.getHsoUn2month(),social.getHsoIn2month());
					if(fMap!=null){
						social.setHsoPensionpay((Float)fMap.get("enconpany")); //设置单位养老
						social.setHsoIndividpay((Float)fMap.get("enpers"));//设置个人养老
						Map<String, Object> ensub = FormulaUtil.formulaById(1,social.getHsoPensionpay(),social.getHsoIndividpay());
						social.setHsoPsubtotal((Float)ensub.get("sum"));
						
						social.setHsoMedicalpay((Float)fMap.get("medicalpany"));//医疗 单位
						social.setHsoMedindividual((Float)fMap.get("medicalpers"));//医疗 个人
						Map<String, Object> mesub = FormulaUtil.formulaById(1,social.getHsoMedicalpay(),social.getHsoMedindividual());
						social.setHsoMedicalsub((Float)mesub.get("sum"));
												
						social.setHsoMediillnes((Float)fMap.get("unconpany"));//大病互助
						social.setHsoMedicalbirth((Float)fMap.get("bornpany"));//生育单位
						
						social.setHsoUnitpay((Float)fMap.get("losepany")); //失业单位
						social.setHsoIndpay((Float)fMap.get("losepers"));//失业个人
						Map<String, Object> unsub = FormulaUtil.formulaById(1,social.getHsoUnitpay(),social.getHsoIndpay());
						social.setHsoUnemploysub((Float)unsub.get("sum"));
					
						social.setHsoInduinjury((Float)fMap.get("injurypany"));//工伤单位
						Map<String, Object> subpany = FormulaUtil.formulaById(1,social.getHsoPensionpay(),social.getHsoMedicalpay(),social.getHsoMediillnes(),
								social.getHsoMedicalbirth(),social.getHsoUnitpay(),social.getHsoInduinjury());
						Map<String, Object> subpers = FormulaUtil.formulaById(1,social.getHsoIndividpay(),social.getHsoMedindividual(),social.getHsoIndpay());
						Map<String, Object> total = FormulaUtil.formulaById(1,social.getHsoPensionpay(),social.getHsoMedicalpay(),social.getHsoMediillnes(),social.getHsoMedicalbirth(),social.getHsoUnitpay(),social.getHsoInduinjury(),social.getHsoIndividpay(),social.getHsoMedindividual(),social.getHsoIndpay());
						
						social.setHsoSubtotalpay((Float)subpany.get("sum"));//公司缴费合计
						social.setHsoSubtotal((Float)subpers.get("sum"));//个人缴费合计
						social.setHsoTotal((Float)total.get("sum"));//总合计
						
						social.setHsoMonth(month);
						dao.add((T) social);
					}
	         }
	         }else{
	        	 msg +="第"+(j+1)+"行记录异常：工号："+iscontinut1+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的考勤没有正常导入（工号跟姓名不匹配）。";
	        	 continue;
	         }
	     }
			return msg;
	}
	
	public List<T> queryScoByHrIdDerpart(TostHrSocialsecurity tosthrsocialsecurity,String derpart,String startM,String endM){
		return getDao().queryScoByHrIdDerpart(tosthrsocialsecurity, derpart,startM, endM);
	}
	
}
